/*************引入模块*****************/
const HtmlWebpackPlugin=require('html-webpack-plugin');
const conf=require('../../../conf.js');
/*************定义变量*****************/
var yourApps=conf.yourApps;
var output=conf.output;
var entry=conf.entry;
var baseJs=entry.baseJs;
var names=baseJs?baseJs.names:[];
var commonJs=conf.commonJs;
var base = "./src/projects/"+conf.projectName+'/';
var after=entry.after?('/'+entry.after):'';
var entrys=[];
/**************执行函数****************/
for(var key of yourApps){
	entry.html=entry.html?(entry.html+'/'):'';
	entrys.push(base+entry.html+key+after+'.html');
}
if(commonJs){
	names.unshift(commonJs);
}
function forHtml(key,type){
	var hwps=this;
	var apps=names.concat([conf.yourApps[key]]);
	var filename=output.html+'/' + conf.yourApps[key] + '.html';
	if(conf.isSingle&&(process.env.NODE_ENV == 'dev')){
		filename='index.html';
	}
	var o={
		template:entrys[key],
		filename:filename,
		chunks:apps,
		favicon:'favicon.ico',
		inject:true,
		minify:false,
		chunksSortMode(chunk1,chunk2){
			var order=names;
			var order1 = order.indexOf(chunk1.names[0]);
	        var order2 = order.indexOf(chunk2.names[0]);
	        order1==-1&&(order1=order.length);
	        order2==-1&&(order2=order.length)
	        return order1-order2;
		}
	};
	hwps.push(new HtmlWebpackPlugin(o));
}
function htmlWP(type){
	var hwps=[];
	if(conf.isSingle){
		forHtml.call(hwps,conf.dev.devStart,type);
	}else{
		for(var key in conf.yourApps){
			forHtml.call(hwps,key,type)
		}
	}
	return hwps;
}
/**************输出接口****************/
module.exports=htmlWP(process.env.NODE_ENV);
